﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biztalk_Alerts.Models;
using System.Data;
using Biztalk_Alerts.DBOps;

namespace Biztalk_Alerts
{
    public partial class Alerts : System.Web.UI.Page
    {
        static IList<ReceiveLocations> rcvLocations = null;
        static IList<SendLoctions> sndLocations = null;
        ReceiveLocations singleLocation = null;
        SendLoctions singleSendLocation = null; 
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                
                //connect and fetch Application Name
                IList<Applications> apps = DBOps.BTOPerations.getApplications();
                grdApplication.DataSource = apps;
                grdApplication.DataBind();

                
            }
        }

 
        private void LoadReceiveLocations()
        {    
           rcvLocations = DBOps.BTOPerations.fetchReceiveLocations(appName);
        }
        private void LoadSendLocations()
        {            
           sndLocations = DBOps.BTOPerations.fetchSendLocations(appName);
        }
       
        protected void ddlReceivePortName_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadReceiveLocations();
            LoadSendLocations(); 

            GrdLocationsDetails.DataSource = rcvLocations;
            GrdLocationsDetails.DataBind();

            
        }


        
        protected void LocationsDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                singleLocation = (ReceiveLocations)e.Row.DataItem;
            }
        }
        string appName = string.Empty;
        protected void grdApplication_SelectedIndexChanged(object sender, EventArgs e)
        {

            appName  = grdApplication.SelectedRow.Cells[0].Text;            

            LoadReceiveLocations();
            LoadSendLocations();

            GrdLocationsDetails.DataSource = rcvLocations;
            GrdLocationsDetails.DataBind();

            GrdSndLocation.DataSource = sndLocations;
            GrdSndLocation.DataBind();
        }

        protected void grdApplication_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
                e.Row.ToolTip = "Click last column for selecting this row.";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (rdoRcvLocation.Checked == true)
            {
                IList<LocationSetup> locSetUpList = new List<LocationSetup>();
                string applicationName = grdApplication.SelectedRow.Cells[0].Text.ToString();
                string receiveLocationName = GrdLocationsDetails.SelectedRow.Cells[0].Text.ToString();

                if (applicationName != string.Empty && receiveLocationName != string.Empty)
                {
                    locSetUpList.Add(new LocationSetup()
                    {
                        AppName = applicationName,
                        mailDuration = 5,
                        others = txtEmail1.Text.ToString().Trim(),
                        Secondary = txtEmail2.Text.ToString().Trim()
                        ,
                        Primary = txtEmail1.Text.ToString().Trim(),
                        email = chkEmail.Checked,
                        restartIfStopped = chkRestartIfStopped.Checked
                        ,
                        ReceiveLocationName = receiveLocationName
                        , type = "Receive"
                    });
                    BTAlertOperations.InsertLocationSetup(locSetUpList);
                }
            }
            else if(rdoSndLocations.Checked== true)
            {
                IList<LocationSetup> locSetUpList = new List<LocationSetup>();
                string applicationName = grdApplication.SelectedRow.Cells[0].Text.ToString();
                string receiveLocationName = GrdSndLocation.SelectedRow.Cells[0].Text.ToString();

                if (applicationName != string.Empty && receiveLocationName != string.Empty)
                {
                    locSetUpList.Add(new LocationSetup()
                    {
                        AppName = applicationName,
                        mailDuration = 5,
                        others = txtEmail1.Text.ToString().Trim(),
                        Secondary = txtEmail2.Text.ToString().Trim()
                        ,
                        Primary = txtEmail1.Text.ToString().Trim(),
                        email = chkEmail.Checked,
                        restartIfStopped = chkRestartIfStopped.Checked
                        ,
                        ReceiveLocationName = receiveLocationName,
                        type = "Send"
                    });
                    BTAlertOperations.InsertLocationSetup(locSetUpList);
                }
            }


        }

        protected void GrdSendLocationDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void GrdSndLocation_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

       
    }
}